/*
    This file is part of the AmaurotMS MapleStory Server, commands OdinMS-based
    Copyleft (L) 2016 - 2019 RonanLana

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation version 3 as published by
    the Free Software Foundation. You may not use, modify or distribute
    this program under any other version of the GNU Affero General Public
    License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
   @Author: MizukiandEna - Refactored command content into modules
*/

package org.gms.client.command.commands.gm0;

import org.gms.client.Character;
import org.gms.client.Client;
import org.gms.client.command.Command;
import org.gms.constants.game.ExpTable;
//import org.gms.util.I18nUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class ExpYangshengGainCommand extends Command {
    {
        //setDescription(I18nUtil.getMessage("ExpYangshengGainCommand.message1"));//可能是介绍
        setDescription("养生状态下您无法打猎获取经验值，但会随着时间积累隐藏经验值。隐藏经验值将在养生状态解除后在打猎过程中转化为经验值。");
    }


    @Override
    public void execute(Client c, String[] params) {
        Character player = c.getPlayer();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.now();
        String timeNow = dateTime.format(formatter);

        if (player.getallowExpGain()){ //如果目前可以获得经验，就是养生还没开
            //allowExpGain = false; //避免有人开了养生又用toggleexp命令，不要写!=
            player.allowExpGainSetFalse();
            player.getAbstractPlayerInteraction().saveOrUpdateCharacterExtendValue("养生状态开启时间",timeNow);
            player.yellowMessage("养生状态开启。您目前处于无法获取经验值的状态。");
        }else{
            //allowExpGain = true;
            player.allowExpGainSetTrue();
            long deltaTimeS = 0;
            //LocalDateTime YangShengStartTime = LocalDateTime.parse(player.getYangShengStartTime(), formatter);
            //LocalDateTime YangShengStartTime = LocalDateTime.parse(player.getAbstractPlayerInteraction().getCharacterExtendValue("养生状态开启时间"));
            LocalDateTime YangShengStartTime = LocalDateTime.parse(player.getAbstractPlayerInteraction().getCharacterExtendValue("养生状态开启时间"),formatter);

            if(YangShengStartTime != null){deltaTimeS = ChronoUnit.SECONDS.between(YangShengStartTime, dateTime);}
            if(deltaTimeS<0 || YangShengStartTime == null){
                player.yellowMessage("您似乎之前toggleexp了，却没有进入养生状态。目前已为您解除toggleexp.");
            }else{
                float SaveBefore70 = 15;
                float SaveBefore120 = 9;
                float SaveBefore150 = 3;
                float SaveBefore200 = 0.6F;
                float SaveSpeed;

                int characterlevel;

                characterlevel = player.getLevel();
                if(characterlevel<70){
                    SaveSpeed = SaveBefore70;
                }else if(characterlevel<120){
                    SaveSpeed = SaveBefore120;
                }else if(characterlevel<150){
                    SaveSpeed = SaveBefore150;
                }else{ SaveSpeed = SaveBefore200; }

                String exp_hide_prototype;
                exp_hide_prototype = player.getAbstractPlayerInteraction().getCharacterExtendValue("养生隐藏经验值");
                if(exp_hide_prototype == null){
                    player.getAbstractPlayerInteraction().saveOrUpdateCharacterExtendValue("养生隐藏经验值","0");
                    exp_hide_prototype = player.getAbstractPlayerInteraction().getCharacterExtendValue("养生隐藏经验值");
                }
                long exp_hide_increase;
                exp_hide_increase = (long)(SaveSpeed*ExpTable.getExpNeededForLevel(characterlevel)*deltaTimeS/(86400));
                long exp_hide_final = exp_hide_increase + Long.parseLong(exp_hide_prototype);
                exp_hide_final = Math.max(Long.parseLong(exp_hide_prototype),Math.min(exp_hide_final,(long)(5*SaveSpeed*ExpTable.getExpNeededForLevel(characterlevel))));
                        //hideExpIncrease(exp_hide_increase);
                player.yellowMessage("养生状态已关闭。您现在可以正常获取经验值。本次养生状态持续"+deltaTimeS+"秒，积累隐藏经验"+exp_hide_increase+"，目前共有隐藏经验"+exp_hide_final);
                //player.setYangShengStartTime("9999-12-31 23:59:59");
                player.getAbstractPlayerInteraction().saveOrUpdateCharacterExtendValue("养生状态开启时间","9999-12-31 23:59:59");
                player.getAbstractPlayerInteraction().saveOrUpdateCharacterExtendValue("养生隐藏经验值",Long.toString(exp_hide_final));
            }
        }
    }

}